﻿using Core;
using RI_Talisman;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection.Emit;
using System.Security.Cryptography;
using UnityEngine;
using Verse;
using Verse.Noise;
using static RimWorld.ColonistBar;

namespace WhoXiuXian
{
    public class HediffComp_AncientCultivatorHediff : HediffComp
    {
        public int tick = 0;

        public int ticksToHeal = 1;
        public Pawn pawn => base.Pawn;
        Map map;
        IntVec3 position;
        public override void CompPostMake()
        {
            base.CompPostMake();
            map = pawn.Map;
            position = pawn.Position;
        }
        public override void CompPostTick(ref float severityAdjustment)
        {
            if (parent.pawn != null)
            {
                if (tick % 40 == 0 && pawn.Map != null)
                {
                    if (pawn.jobs.curJob.def == JobDefOf.AttackMelee && pawn.stances.FullBodyBusy)
                    {
                        pawn.stances.CancelBusyStanceHard();
                        pawn.stances.stunner.StopStun();
                    }
                }

                if (pawn.Faction == Faction.OfAncientsHostile)
                {
                    if ((tick % ticksToHeal == 0 || pawn.Downed) && !pawn.Dead)
                    {
                        foreach (Hediff hediff in pawn.health.hediffSet.hediffs)
                        {
                            if ((hediff.TendableNow() && (hediff.GetType() == typeof(Hediff_Injury))) || hediff.GetType() == typeof(Hediff_MissingPart)|| hediff.def.isBad)
                            {
                                hediff.Severity = 0;
                                EnergyRootUtility.GetRoot(pawn).energy.energy= EnergyRootUtility.GetRoot(pawn).energy.CurrentDef.MaxEnergy;
                                if (ticksToHeal <= 80 && !pawn.Downed)
                                {
                                    ticksToHeal++;
                                    tick = 0;
                                }
                                break;
                            }
                        }
                    }
                    if (pawn.Downed)
                    {
                        pawn.Kill(new DamageInfo(DamageDefOf.Bomb,800)); ;
                    }
                }
                map = pawn.Map;
                position = pawn.Position;
                tick++;
            }
        }
        public override void Notify_PawnDied(DamageInfo? dinfo, Hediff culprit = null)
        {
            if (pawn.Faction == Faction.OfAncientsHostile)
            {
                Thing armor = GenSpawn.Spawn(RI_T_DefOfs.RI_Apparel_AncientCultivatorArmor, position, map);
                Thing helmet = GenSpawn.Spawn(RI_T_DefOfs.RI_Apparel_AncientCultivatorHelmet, position, map);
                Thing book = GenSpawn.Spawn(RI_T_DefOfs.RI_BookAbility_RI_AncientCultivatorPlayer, position, map);
                armor.TryGetComp<CompQuality>().SetQuality((QualityCategory)6, (ArtGenerationContext?)1);
                helmet.TryGetComp<CompQuality>().SetQuality((QualityCategory)6, (ArtGenerationContext?)1);
                pawn.apparel.DestroyAll();
                parent.Severity = 0;
                LetterDef baseLetterDef = (LetterDefOf.PositiveEvent);
                string text = string.Format("由于不适应灵气稀薄的环境，古修者的灵芽因负荷过大而爆裂了，但他的护甲仍旧崭新如初...").Translate();
                ChoiceLetter choiceLetter = LetterMaker.MakeLetter("战胜古修者".Translate(), text, LetterDefOf.PositiveEvent);
                Find.LetterStack.ReceiveLetter(choiceLetter);
            }
        }

        public override void CompExposeData()
        {
            base.CompExposeData();
        }

    }
}